﻿var selectcity = ""
var provinceName = [];
var provinceid = [];
var selectproduct = ""
var productName = [];
var productid = [];
var selectcrop = ""
var cropName = [];
var cropid = [];
//获取产品列表
$.ajax({
    url: fixUrl("api/Product/GetProductCategoryList"),
    success: function (data) {
        $("#productCategory").html('');
        for (var i = 0; i < data.length; i++) {
            $("#productCategory").append("<option value='" + data[i].ID + "'>" + data[i].Name + "</option>");
        }
    }
})

//农作物
$.ajax({
    url: fixUrl("api/Crop/GetAllActiveListCrops"),
    type: "get",
    success: function (data) {
        $.each(data, function (index, value) {
            //if (value.Name != "请选择") {
            cropName.push(value.Name);
            cropid.push(value.ID);
            //}
        })
        $("#CropName").val("");
        $("#CropName_val").val("");
        $("#CropName").picker({
            cols: [
              {
                  textAlign: 'center',
                  displayValues: cropName,
                  values: cropid
              }

            ],
            formatValue: function (picker, value, displayValue) {
                selectcrop = displayValue[0];
                //$(picker.input).val(displayValue[0]);
                $("#CropName_val").val(value[0]);
            }
        });
    }
})
//获取省份
$.ajax({
    url: fixUrl("api/Promoter/GetProvinceByPromoterId"),
    success: function (data) {
        provinceName.push("请选择");
        provinceid.push(0);
        $.each(data, function (index, value) {
            provinceName.push(value.Name);
            provinceid.push(value.ID);
        })
    }
})
$("#provincepick").picker({
    cols: [
      {
          textAlign: 'center',
          displayValues: provinceName,
          values: provinceid
      }
    ],
    formatValue: function (picker, value, displayValue) {
        selectcity = displayValue[0];
        //$(picker.input).val(displayValue[0]);
        $("#hid_provincepick").val(value[0]);
    }
});

function UpdateProvince() {
    if ($("#provincepick").val() == "undefined") {
        $("#provincepick").val(selectcity);
    }
}

function UpdateProduct() {
    if ($("#ProductName").val() == "undefined") {
        $("#ProductName").val(selectproduct);
    }
}

function UpdateCrop() {
    if ($("#CropName").val() == "undefined") {
        $("#CropName").val(selectcrop);
    }
}

//选择产品列表
function GetProductByCategory() {
    $.ajax({
        url: fixUrl("api/Product/GetActiveProductListByProductCategory"),
        type: "get",
        data: { id: $("#productCategory").val() },
        success: function (data) {
            productName.splice(0, productName.length);
            productid.splice(0, productid.length);
            $.each(data, function (index, value) {
                //if (value.Name != "请选择") {
                productName.push(value.Name);
                productid.push(value.ID);
                //}
            })
            $("#ProductName").val("");
            $("#ProductName_val").val("");
            $("#ProductName").picker({
                cols: [
                  {
                      textAlign: 'center',
                      displayValues: productName,
                      values: productid
                  }

                ],
                formatValue: function (picker, value, displayValue) {
                    selectproduct = displayValue[0];
                    //$(picker.input).val(displayValue[0]);
                    $("#ProductName_val").val(value[0]);
                }
            });
        }
    })
}
$("#ProductName").click(function () {
    if ($("#productCategory").val() > 0) {
    } else {
        //$("#ProductName").picker("close");
        $.toast("请先选择产品类别");
    }
})
var allowsubmit = true;

$("#NextStep").on("click", function () {
    if ($("#productCategory").val() <= 0) {
        $.toast("请先选择产品类别");
    } else if ($("#ProductName_val").val() <= 0 || $("#ProductName_val").val() == "") {
        $.toast("请先选择产品");
    } else if ($("#CropName_val").val() < 0 || $("#CropName_val").val() == "") {
        $.toast("请先选择农作物");
    } else if ($("#hid_provincepick").val() < 0 || $("#hid_provincepick").val() == "") {
        $.toast("请先选择农作物");
    } else if ($("#hid_provincepick").val() <= 0) {
        $.toast("请先选择省份");
    } else {
        if (allowsubmit == true) {
            allowsubmit = false;
            var postdata = { basicInfo: {}, trialInfo: {} }
            postdata.trialInfo.cropID = $("#CropName_val").val();
            postdata.trialInfo.cropName = $("#CropName").val();
            postdata.trialInfo.productID = $("#ProductName_val").val();
            postdata.trialInfo.productName = $("#ProductName").val();
            postdata.trialInfo.provinceID = $("#hid_provincepick").val();
            $.ajax({
                url: fixUrl("api/Trial/AddNewTrialByPromoter"),
                data: postdata,
                type: "post",
                //beforeload: $.showPreloader('正在保存页面数据...'),
                success: function (data) {
                    //$.hidePreloader();
                    $.toast("提交成功");
                    if (data > 0) {
                        location.href = fixUrl("TrialMobile/ShiFandian?id=" + data);
                    }
                },
                error: function (data) {
                    allowsubmit = true;
                    //$.hidePreloader();
                    if (data.status == "401") {
                        $.alert('登录超时');
                    } else {
                        $.alert('保存失败,请重试');
                    }
                }
            })
        }
        //$.router.load("#BasicInfo");
    }
})

